﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using LuaFramework;
using LuaInterface;
using System;

public class BaseEventListener : MonoBehaviour
{
	public LuaFunction func = null;
	public void OnFunc(PointerEventData eventData)
	{
		if (func != null) {
			func.Call (this.transform, eventData);
		}
	}
}

public class OnClickFunc : BaseEventListener, IPointerClickHandler
{
	Selectable selectable;
    void Awake()
    {
        selectable = GetComponent<Selectable>();
    }
    
	public void OnPointerClick(PointerEventData eventData)
	{
		if (!eventData.dragging && (selectable == null || (selectable.enabled && selectable.interactable))) {
			OnFunc (eventData);
		}
	}
}

public class OnDownFunc : BaseEventListener, IPointerDownHandler
{
	public void OnPointerDown(PointerEventData eventData)
	{
		OnFunc (eventData);
	}
}

public class OnUpFunc : BaseEventListener, IPointerUpHandler
{
	public void OnPointerUp(PointerEventData eventData)
	{
		OnFunc (eventData);
	}
}

public class OnDragFunc : BaseEventListener, IDragHandler
{
	public void OnDrag(PointerEventData eventData)
	{
		OnFunc (eventData);
	}
}

public class OnBeginDragFunc : BaseEventListener, IBeginDragHandler, IDragHandler
{
	public void OnBeginDrag(PointerEventData eventData)
	{
		OnFunc (eventData);
	}
	public void OnDrag(PointerEventData eventData)
	{
	}
}

public class OnEndDragFunc : BaseEventListener, IEndDragHandler
{
	public void OnEndDrag(PointerEventData eventData)
	{
		OnFunc (eventData);
	}
}

public class OnMoveFunc : BaseEventListener, IDragHandler
{
	public void OnDrag(PointerEventData eventData)
	{
		var pos1 = eventData.pressEventCamera.ScreenToWorldPoint (eventData.position - eventData.delta);
		var pos2 = eventData.pressEventCamera.ScreenToWorldPoint (eventData.position);
		this.transform.position = this.transform.position + (pos2 - pos1);
		OnFunc (eventData);
	}
}

public class OnEnterFunc : BaseEventListener, IPointerEnterHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        OnFunc(eventData);
    }
}

public class OnExitFunc : BaseEventListener, IPointerExitHandler
{
    public void OnPointerExit(PointerEventData eventData)
    {
        OnFunc(eventData);
    }
}

public class PointerEventListener : MonoBehaviour
{
	private void SetEventListener<T>(LuaFunction func) where T : BaseEventListener
	{
		T handler = gameObject.GetComponent<T> ();
		if (handler == null) {
			handler = gameObject.AddComponent<T> ();
		}
		handler.func = func;
	}

	public LuaFunction onClick{
		set{
			SetEventListener<OnClickFunc>(value);
		}
	}
	public LuaFunction onDown{
		set{
			SetEventListener<OnDownFunc>(value);
		}
	}
	public LuaFunction onUp{
		set{
			SetEventListener<OnUpFunc>(value);
		}
	}

	public LuaFunction onDrag{
		set{
			SetEventListener<OnDragFunc>(value);
		}
	}

	public LuaFunction onBeginDrag{
		set{
			SetEventListener<OnBeginDragFunc>(value);
		}
	}

	public LuaFunction onEndDrag{
		set{
			SetEventListener<OnEndDragFunc>(value);
		}
	}
	public LuaFunction onMove{
		set{
            SetEventListener<OnMoveFunc>(value);
        }
    }
    public LuaFunction onEnter
    {
        set
        {
            SetEventListener<OnEnterFunc>(value);
        }
    }
    public LuaFunction onExit
    {
        set
        {
            SetEventListener<OnExitFunc>(value);
        }
    }
}
